package com.jzo2o.customer.controller.consumer;

import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.model.Result;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.customer.service.ICommonUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 普通用户相关接口
 *
 * @author itcast
 * @create 2023/7/7 19:34
 **/
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 用户地址相关接口")
public class AddressBookController {

    @Autowired
    private IAddressBookService addressBookService;

    /**
     * 获取默认地址
     *
     * @return
     */
    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    public AddressBookResDTO getDefaultAddress() {
        return addressBookService.getDefaultAddress();
    }

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result add(@RequestBody AddressBookUpsertReqDTO addressBook) {
        addressBookService.add(addressBook);
        return Result.ok();
    }


    /**
     * 分页查询地址
     *
     * @param addressBookPageQueryReqDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询地址")
    //todo 分页查询需要分析，请求参数非必须的情况下，怎么接收参数
    public Result<PageResult<AddressBookResDTO>> page(AddressBookPageQueryReqDTO addressBookPageQueryReqDTO) {
        return Result.ok(addressBookService.findByPage(addressBookPageQueryReqDTO));
    }


    /**
     * 查询地址详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("查询地址详细信息")
    public AddressBookResDTO query(@PathVariable Long id) {
        return addressBookService.findById(id);
    }

    /**
     * 修改地址
     *
     * @param id
     * @param addressBook
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation("修改地址")
    public Result update(@PathVariable Long id, @RequestBody AddressBookUpsertReqDTO addressBook) {
        addressBookService.updateAddressById(id, addressBook);
        return Result.ok();
    }

    /**
     * 批量删除地址
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    @ApiOperation("批量删除地址")
    public Result batchDelete(@RequestBody List<Long> ids) {
        addressBookService.removeAddressByIds(ids);
        return Result.ok();
    }


    /**
     * 设置默认地址
     * @param id
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(Long id, Integer flag) {
        addressBookService.setDefaultAddress(id, flag);
        return Result.ok();
    }
}
